<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;

/**
 * 采购单设置 设置
 * @package app\erp\model
 */
class ErpGoodsPurchaseOrder extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_goods_purchase_order';
	
	/**
     * 添加数据
     * @param string  	title				
	 * @param int		supplier_id			 供应商id
	 * @param int 		procurement_method	 采购方式 1现购 2赊购
	 * @param int		arrival_warehouse_id 到货仓id
	 * @param string 	advance_charge		 预付款
	 * @param int 		arrival_days		 到货天数
	 * @param string	remark				 备注
	 * @param string 	handled_by			 经办人
     * @author shiqiang
     * @return array_out(code, message, (int)id)
     */
    public function addData($user_id, $title, $supplier_id, $procurement_method, $arrival_warehouse_id, $advance_charge, $arrival_days, $remark, $handled_by, $code, $back_date, $purchase_status = 1){
        $data['uid']        					= $user_id;
        $data['title']        					= $title;
        $data['supplier_id']         			= $supplier_id;
        $data['procurement_method']   			= $procurement_method;
        $data['arrival_warehouse_id'] 			= $arrival_warehouse_id;
        $data['advance_charge'] 				= $advance_charge;
        $data['arrival_days'] 					= $arrival_days;
        $data['remark'] 						= $remark;
        $data['handled_by'] 					= $handled_by;
        $data['code'] 							= $code;
        $data['back_date'] 						= $back_date;
        $data['purchase_status'] 				= $purchase_status;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this -> id);
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 供应商 关联载入（一对一）
     */
    public function getErpSupplier(){
        return $this -> hasOne('app\erp\model\ErpSupplier','id', 'supplier_id');
    }
	
	/**
     * 仓库 关联载入（一对一）
     */
    public function getErpWarehouse(){
        return $this -> hasOne('app\erp\model\ErpWarehouse','id', 'arrival_warehouse_id');
    }
}
